package LeetCode;

public class Num2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode pre=null,cur=null;
        int sum=0;
        while (l1!=null || l2!=null){
            int n1=l1!=null?l1.val:0;
            int n2=l2!=null?l2.val:0;
            if(pre==null){
                pre=cur=new ListNode((n1+n2+sum)%10);
            }else {
                cur.next=new ListNode((n1+n2+sum)%10);
                cur=cur.next;
            }
            sum=(n1+n2+sum)/10;
            if(l1!=null) {
                l1 = l1.next;
            }
            if(l2!=null){
                l2=l2.next;
            }
        }
        if(sum>0) {
            cur.next = new ListNode(sum);
        }
        return pre;
    }
}
